package cc.telecomdigital.tdstock.trading.widget;

import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Services.CommunicTools;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AppDialogPassword extends Dialog implements
	Button.OnClickListener, OnCancelListener
{
	public interface IAppDialogPassword
	{
		/** What to do when an OK is pressed **/		
		void OnOK_Click(DialogInterface iDialog);

		/** What to do when Canel is pressed **/			
		void OnCancel_Click(DialogInterface iDialog);

		/** What to do when user pressed back button **/			
		void OnDialogCancel(DialogInterface iDialog);
	}

	private IAppDialogPassword	interfaceHandle;
	private EditText			passwordObj;

	/** Create a Password entry dialog box **/
	public AppDialogPassword(Context iContext)
	{
		super(iContext, R.style.TradeDialog_Style);
		setContentView(R.layout.app_dialog_password);

		Button buttonObj;

		buttonObj = (Button) findViewById(R.id.app_dialog_password_OKButton);
		buttonObj.setOnClickListener(this);

		buttonObj = (Button) findViewById(R.id.app_dialog_password_CancelButton);
		buttonObj.setOnClickListener(this);

		passwordObj = (EditText) findViewById(R.id.app_dialog_password_Edit);
		passwordObj.setOnFocusChangeListener(new View.OnFocusChangeListener()
		{
			public void onFocusChange(View iViewObj, boolean hasFocus)
			{
				if (hasFocus)
					ShowKeyBoard(iViewObj);
			}
		});

		passwordObj
				.setOnEditorActionListener(new TextView.OnEditorActionListener()
				{
					@Override
					public boolean onEditorAction(TextView v,
							int actionId, KeyEvent event)
					{
						if (actionId == EditorInfo.IME_ACTION_DONE)
						{
							// Close Keyboard
							HideKeyBoard();
							onClick(findViewById(R.id.app_dialog_password_OKButton));
							return true;
						}
						return false;
					}
				});

		this.setOnCancelListener(this);
	}

	/** Set the Prompt content; "Please enter the password" is the default **/
	public void SetTextContent(String iContent)
	{
		TextView textViewObj;

		textViewObj = (TextView) findViewById(R.id.app_dialog_password_Header);
		iContent = iContent.replace("\\n", "\n");		
		textViewObj.setText(iContent);
	}

	/** Set the pre-entered password, password are covered **/
	public void SetPasswordContent(String iContent)
	{
		passwordObj.setText(iContent);
	}

	/** Get the password content, opened **/
	public String GetPasswordContent()
	{
		return passwordObj.getText().toString();
	}

	/** Show the Dialog Box with return interface, the dialog will be dismiss when either of action
	   is done. iShowKeyboard could bring the keyboard up after being shown **/
	public void show(IAppDialogPassword iInterface, boolean iCancelable,
			boolean iShowKeyboard)
	{
		interfaceHandle = iInterface;
		this.setCancelable(iCancelable);

		this.show();

		if (iShowKeyboard)
		{
			Message msg;

			msg = new Message();
			msg.what = SHOW_KEYBOARD;
			handler.sendMessage(msg);
		}
	}

	private final int	SHOW_KEYBOARD	= 1;
	private Handler	handler		= new Handler()
								{
									@Override
									public void handleMessage(
											Message msg)
									{
										switch (msg.what)
										{
											case SHOW_KEYBOARD:
												InputMethodManager inputMgr = (InputMethodManager) getContext()
														.getSystemService(
																Context.INPUT_METHOD_SERVICE);
												inputMgr.showSoftInput(
														passwordObj,
														2);
												break;
										}
									}
								};

	@Override
	public void onClick(View iViewObj)
	{
		switch (iViewObj.getId())
		{
			case R.id.app_dialog_password_OKButton:
			{
				String password;

				password = passwordObj.getText().toString();
				if (!CommunicTools.isASCIIString(password))				
				{
					Animation animatAction = AnimationUtils.loadAnimation(
							getContext(), R.anim.shake);
					animatAction.setDuration(300);
					passwordObj.startAnimation(animatAction);
					return;
				}
				HideKeyBoard();
				this.dismiss();
				if (interfaceHandle != null)
					interfaceHandle.OnOK_Click(this);
			}
				break;

			case R.id.app_dialog_password_CancelButton:
				HideKeyBoard();
				this.dismiss();
				if (interfaceHandle != null)
					interfaceHandle.OnCancel_Click(this);
				break;

		}
	}

	@Override
	public void onCancel(DialogInterface iDialog)
	{
		// TODO Auto-generated method stub
		iDialog.dismiss();
		if (interfaceHandle != null)
			interfaceHandle.OnDialogCancel(this);
	}

	protected void ShowKeyBoard(View iViewObj)
	{
		InputMethodManager mgr;

		if (iViewObj == null)
			return;

		mgr = (InputMethodManager) getContext().getSystemService(
				Context.INPUT_METHOD_SERVICE);
		mgr.showSoftInput(iViewObj, InputMethodManager.SHOW_FORCED);
	}

	protected void HideKeyBoard()
	{
		InputMethodManager mgr;
		View viewObj;

		viewObj = getCurrentFocus();
		mgr = (InputMethodManager) getContext().getSystemService(
				Context.INPUT_METHOD_SERVICE);
		if (mgr.isActive() && (viewObj != null))
			mgr.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
					0);
	}
}
